﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CarLockRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-12-02
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    /// 仓储层
    /// </summary>	
    public class CarLockRepository : BaseRepository<CarLock>
    {

        #region 构造

        public CarLockRepository() : base() { }

        public CarLockRepository(string connectionString) : base(connectionString) { }

        public CarLockRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        public Page<CarLock> Pages(int userId, int pageIndex, int pageSize)
        {
            string sql = "SELECT * FROM CarLock WITH(NOLOCK) WHERE IsActive=1 AND UserId=@UserId";
            return Pages(pageIndex, pageSize, sql, new { UserId = userId });
        }


        public Page<CarLockDto> Pages(int pageIndex, int pageSize, CarLockSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT T1.*,T2.UserName,T2.Mobile FROM [CarLock] T1 WITH(NOLOCK) Left Join UserInfo T2 on T1.UserId=T2.UserId WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }
            sb.Append(" ORDER BY T1.ID DESC");
            return dbContext.Connection.Page<CarLockDto>(pageIndex, pageSize, sb.ToString(), dparams);
        }
        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(CarLockSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {

            if (!string.IsNullOrWhiteSpace(model.LockNum))
            {
                sb.Append(" AND charindex(@LockNum,T1.LockNum)>0");
                dparams.Add("LockNum", model.LockNum, System.Data.DbType.String);
            }
            if (model.LockStatus >= 0)
            {
                sb.Append(" AND T1.LockStatus=@LockStatus");
                dparams.Add("LockStatus", model.LockStatus, System.Data.DbType.Int32);
            }
            if (model.ProvinceID > 0)
            {
                sb.Append(" AND T1.ProvinceID=@ProvinceID");
                dparams.Add("ProvinceID", model.ProvinceID, System.Data.DbType.Int32);
            }

            if (model.CityID > 0)
            {
                sb.Append(" AND T1.CityID=@CityID");
                dparams.Add("CityID", model.CityID, System.Data.DbType.Int32);
            }
            if (model.DistrictID > 0)
            {
                sb.Append(" AND T1.DistrictID=@DistrictID");
                dparams.Add("DistrictID", model.DistrictID, System.Data.DbType.Int32);
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteLock(int id)
        {
            string sql = "UPDATE CarLock Set IsActive=0 WHERE ID=@ID";
            return Execute(sql, new { ID = id });
        }
    }
}